package cn.wuxing.mediation.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.wuxing.mediation.common.utils.SecurityUtils;
import cn.wuxing.mediation.system.domain.vo.MessageNoticeVo;
import cn.wuxing.mediation.system.mapper.SysUserRoleMapper;
import cn.wuxing.mediation.system.service.ISMSGNotificationsService;
import org.springframework.stereotype.Service;
import cn.wuxing.mediation.system.mapper.MessageNotificationsMapper;
import cn.wuxing.mediation.system.domain.MessageNotifications;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author mediation
 * @date 2024-10-08
 */
@Service
public class ISMSGNotificationsServiceImpl implements ISMSGNotificationsService
{
    @Resource
    private MessageNotificationsMapper cmMessageNotificationsMapper;

    @Resource
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MessageNotifications selectMessageNotificationsById(Long id)
    {
        return cmMessageNotificationsMapper.selectMessageNotificationsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param messageNotifications 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MessageNotifications> selectMessageNotificationsList(MessageNotifications messageNotifications)
    {
        return cmMessageNotificationsMapper.selectMessageNotificationsList(messageNotifications);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param messageNotifications 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertMessageNotifications(MessageNotifications messageNotifications)
    {
        messageNotifications.setClickFlag("0");
        return cmMessageNotificationsMapper.insertMessageNotifications(messageNotifications);
    }

    @Override
    public int updateMessageNotifications(MessageNotifications messageNotifications) {
        messageNotifications.setClickFlag("1");
        return cmMessageNotificationsMapper.updateMessageNotifications(messageNotifications);
    }

    @Override
    public List<Object> disputeNotice() {
        List<Object> dataResult=new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        if(userId==1){//超管权限 显示全部
            dataResult=getMessageNotificationsByAdmin();
            return dataResult;
        }
        //综窗区级
        Long[] userIdsQu=cmMessageNotificationsMapper.selectSynthesisIdsByIdQu(userId);//区级
        if(userIdsQu!=null&&userIdsQu.length>0){
            dataResult=getMessageNotificationsBySynthesis(userIdsQu);
            Long[] ids=new Long[]{userId};
            dataResult.addAll(getMessageNotificationsByMediator(ids));
            return dataResult;
        }
        //综窗乡镇
        Long[] userIdsXiang=cmMessageNotificationsMapper.selectSynthesisIdsByIdXiang(userId);//乡镇
        if(userIdsXiang!=null&&userIdsXiang.length>0){
            dataResult=getMessageNotificationsBySynthesis(userIdsXiang);
            Long[] ids=new Long[]{userId};
            dataResult.addAll(getMessageNotificationsByMediator(ids));
            return dataResult;
        }
        //专窗
        Long[] windows=cmMessageNotificationsMapper.selectWindowIds(userId);
        if(windows!=null&&windows.length>0){
            dataResult.addAll(getMessageNotificationsByWindowIds(windows));
            Long[] ids=new Long[]{userId};
            dataResult.addAll(getMessageNotificationsByMediator(ids));
            return dataResult;
        }
        //乡镇治理中心
        Long[] townshipCenter = cmMessageNotificationsMapper.selectTownshipCenter(userId);
        if(townshipCenter!=null&&townshipCenter.length>0){
            dataResult.addAll(getMessageNotificationsByTownShip(townshipCenter));
            Long[] ids=new Long[]{userId};
            dataResult.addAll(getMessageNotificationsByMediator(ids));
            return dataResult;
        }

        //调解员账号
        Long[] ids=new Long[]{userId};
        dataResult.addAll(getMessageNotificationsByMediator(ids));
        return dataResult;
    }

    @Override
    public int disputeNoticeRead(Long noticeId) {
        int result=cmMessageNotificationsMapper.updateMessageNotificationsRead(noticeId);
        return result;
    }

    //超管
    private List<Object> getMessageNotificationsByAdmin() {
        List<Object> dataResult=new ArrayList<>();
        Long[] synthesisIds=cmMessageNotificationsMapper.selectSynthesisIds();
        //综窗
        if(synthesisIds.length==0){
            synthesisIds=new Long[]{9999999999999999l};
        }
        List<MessageNoticeVo>messageNoticeSynthesisNot=cmMessageNotificationsMapper
                .getDisputeNoticeSynthesisNot(synthesisIds);
        List<MessageNoticeVo>messageNoticeSynthesisOrd=cmMessageNotificationsMapper
                .getDisputeNoticeSynthesisOrd(synthesisIds);
        List<MessageNoticeVo>messageNoticeSynthesisPre=cmMessageNotificationsMapper
                .getDisputeNoticeSynthesisPre(synthesisIds);
        dataResult.addAll(messageNoticeSynthesisNot);
        dataResult.addAll(messageNoticeSynthesisOrd);
        dataResult.addAll(messageNoticeSynthesisPre);

        //窗口类型非纠纷通知
        Long[] windowIds = cmMessageNotificationsMapper.selectWindowIdsByAdmin();
        if(windowIds.length==0){
            windowIds=new Long[]{9999999999999999l};
        }
        List<MessageNoticeVo> messageNoticeVosWindowsNot=cmMessageNotificationsMapper
                .getDisputeNoticeWindowNot(windowIds);
        //窗口类型一般纷通知
        List<MessageNoticeVo> messageNoticeVosWindowsOrd=cmMessageNotificationsMapper
                .getDisputeNoticeWindowOrd(windowIds);
        //窗口类型诉前纷通知
        List<MessageNoticeVo> messageNoticeVosWindowsPre=cmMessageNotificationsMapper
                .getDisputeNoticeWindowPre(windowIds);
        dataResult.addAll(messageNoticeVosWindowsNot);
        dataResult.addAll(messageNoticeVosWindowsOrd);
        dataResult.addAll(messageNoticeVosWindowsPre);

        //乡镇治理中心
        Long[] townshipCenterIds=cmMessageNotificationsMapper.selectTownshipCenter(null);
        if(townshipCenterIds.length==0){
            townshipCenterIds=new Long[]{999999999999999l};
        }
        List<Object> messageNotificationsByTownShip=getMessageNotificationsByTownShip(townshipCenterIds);
        dataResult.addAll(messageNotificationsByTownShip);

        //调解员
        Long[] mediators=cmMessageNotificationsMapper.selectMediators();
        List<Object> notificationsByMediator = getMessageNotificationsByMediator(mediators);
        dataResult.addAll(notificationsByMediator);
        return dataResult;
    }

    //乡镇治理中心
    private List<Object> getMessageNotificationsByTownShip(Long[] townshipCenterIds) {
        List<Object> dataResult=new ArrayList<>();
        List<MessageNoticeVo>messageNoticeNot=cmMessageNotificationsMapper
                .getDisputeNoticeTownShipNot(townshipCenterIds);
        List<MessageNoticeVo>messageNoticeOrd=cmMessageNotificationsMapper
                .getDisputeNoticeTownShipOrd(townshipCenterIds);
        List<MessageNoticeVo>messageNoticePre=cmMessageNotificationsMapper
                .getDisputeNoticeTownShipPre(townshipCenterIds);
        dataResult.addAll(messageNoticeNot);
        dataResult.addAll(messageNoticeOrd);
        dataResult.addAll(messageNoticePre);
        return dataResult;
    }

    //综窗
    private List<Object> getMessageNotificationsBySynthesis(Long[] userId){
        List<Object> dataResult=new ArrayList<>();
        List<MessageNoticeVo>messageNoticeSynthesisNot=cmMessageNotificationsMapper
                .getDisputeNoticeSynthesisNot(userId);
        List<MessageNoticeVo>messageNoticeSynthesisOrd=cmMessageNotificationsMapper
                .getDisputeNoticeSynthesisOrd(userId);
        List<MessageNoticeVo>messageNoticeSynthesisPre=cmMessageNotificationsMapper
                .getDisputeNoticeSynthesisPre(userId);
        dataResult.addAll(messageNoticeSynthesisNot);
        dataResult.addAll(messageNoticeSynthesisOrd);
        dataResult.addAll(messageNoticeSynthesisPre);
        return dataResult;
    }

    //专窗
    private List<Object> getMessageNotificationsByWindowIds(Long[] windows){
        List<Object> dataResult=new ArrayList<>();
        List<MessageNoticeVo> messageNoticeVosWindowsNot=cmMessageNotificationsMapper
                .getDisputeNoticeWindowNot(windows);
        //窗口类型一般纷通知
        List<MessageNoticeVo> messageNoticeVosWindowsOrd=cmMessageNotificationsMapper
                .getDisputeNoticeWindowOrd(windows);
        //窗口类型诉前纷通知
        List<MessageNoticeVo> messageNoticeVosWindowsPre=cmMessageNotificationsMapper
                .getDisputeNoticeWindowPre(windows);
        dataResult.addAll(messageNoticeVosWindowsNot);
        dataResult.addAll(messageNoticeVosWindowsOrd);
        dataResult.addAll(messageNoticeVosWindowsPre);
        return dataResult;
    }

    //调解员
    private List<Object> getMessageNotificationsByMediator(Long[] userIds){
        List<Object> dataResult=new ArrayList<>();
        if(userIds==null||userIds.length==0){
            userIds=new Long[]{99999999999999l};
        }
        //一般纠纷
        List<MessageNoticeVo> disputeNoticeOrd=cmMessageNotificationsMapper.getDisputeNoticeOrdByMediator(userIds);
        //诉前纠纷
        List<MessageNoticeVo> disputeNoticePre=cmMessageNotificationsMapper.getDisputeNoticePreByMediator(userIds);
        dataResult.addAll(disputeNoticeOrd);
        dataResult.addAll(disputeNoticePre);
        return dataResult;
    }

}
